<?php

App::uses('AppController', 'Controller');

class UnidadesController extends AppController {

    public $layout = 'hospital';    
    /**
     * index method
     *
     * @return void
     */
    public function beforeFilter()
    {
        parent::beforeFilter();
        //$this->Auth->allow();
    }
    
    public function index() {
        $unidades = $this->Unidade->find('all', array('recursive'=>-1));
        $this->set(compact('unidades'));
    }

    /**
     * view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function view($id = null) {
        if (!$this->Unidade->exists($id)) {
            throw new NotFoundException(__('Invalid unidade'));
        }
        $options = array('conditions' => array('Unidade.' . $this->Unidade->primaryKey => $id));
        $this->set('unidade', $this->Unidade->find('first', $options));
    }

    /**
     * add method
     * //debug($this->request->data);exit;
     * @return void
     */
    public function add() {
        if ($this->request->is('post')) {      
            //debug($this->request->data);exit;
            $this->Unidade->create();
            if ($this->Unidade->save($this->request->data)) {
                $this->Session->setFlash(__('The unidad has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The unidad could not be saved. Please, try again.'));
            }
        } 
     }

    /**
     * edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function edit($id = null) {
        if (!$this->Unidade->exists($id)) { 
            throw new NotFoundException(__('invalid unidad'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            debug($this->request->data);exit;
            if ($this->Unidade->save($this->request->data)) {
                $this->Session->setFlash(__('The uniddad has been modife'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The unidad could not be saved. Please, try again.'));
            }
        } else {
            $options = array('conditions' => array('Unidade.' . $this->Unidade->primaryKey => $id));
            $this->request->data = $this->Unidade->find('first', $options);
        }
    }

    /**
     * delete method
     *
     * @throws NotFoundException
     * @throws MethodNotAllowedException
     * @param string $id
     * @return void
     */
    public function delete($id = null) {
        $this->Unidade->id = $id;
        if (!$this->Unidade->exists()) {
            throw new NotFoundException(__('Invalid unidad'));
        }
        $this->request->onlyAllow('post', 'delete');
        if ($this->Unidade->delete()) {
            $this->Session->setFlash(__('Unidad deleted'));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Unidad was not deleted'));
        $this->redirect(array('action' => 'index'));
    }   
}
